<?php


namespace App\Http\Services;


use App\Http\Model\ContentCheck;
use App\Http\Model\Hint;
use App\Http\Model\Room;
use App\Http\Model\RoomImageCheck;
use App\Http\Model\RoomUser;

class RoomImageCheckService extends CommonServices
{
    protected $modelRoomImageCheck;
    protected $roomModel;
    protected $roomUserModel;
    protected $contentCheckModel;

    public function __construct()
    {
        parent::__construct();
        $this->modelRoomImageCheck = new RoomImageCheck();
        $this->roomModel = new Room();
        $this->roomUserModel = new RoomUser();
        $this->contentCheckModel = new ContentCheck();
    }

    public function setImgUrl()
    {
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $this->I('room_id', '房间id', 'int', true);
            $this->I('image', '图像url', 'string', true);

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化
            $user = $this->user->get_current_user();
            $user_id = $user['user_id'];
            $room_id = $this->requestData['room_id'];
            $role = $this->roomModel->get_role($room_id, $user_id);
            if ($role == 0) {
                $code = Hint::AUTH_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }
            $images = explode(',',$this->requestData['image']);

            foreach ($images as $img){
                $temp['room_id'] = $this->requestData['room_id'];
                $temp['user_id'] = $user_id;
                $temp['image'] = $img;
                $id = $this->modelRoomImageCheck->CUDTable($temp ,'insert');
                if($id){
                    $this->contentCheckModel->create($user_id ,13 ,$img ,'',$this->requestData['room_id'],'',$id);
                }
            }

        }while (false);

        return compact('code', 'msg', 'data');
    }

    public function getImgList(){
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $this->I('room_id', '房间id', 'int', true);
            $this->I('page', 'page', 'int', false);
            $this->I('limit', 'limit', 'int', false);

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            $room_id = $this->requestData['room_id'];
            $user = $this->user->get_current_user();
            $role = $this->roomModel->get_role($room_id, $user['user_id']);
            if ($role == 0) {
                $code = Hint::AUTH_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }
            $page = $this->requestData['page'] ?? 1;// 页数
            $limit = $this->requestData['limit'] ?? 10;// 条数

            $data['list'] = $this->modelRoomImageCheck->getImgList($room_id ,$page ,$limit);
        }while (false);

        return compact('code', 'msg', 'data');
    }

    public function delImg(){
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $this->I('id', 'id', 'string', true);
            $this->I('room_id', '房间id', 'int', true);

            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            extract($variables); //数组参数化

            $user = $this->user->get_current_user();
            $role = $this->roomModel->get_role($this->requestData['room_id'], $user['user_id']);
            if ($role == 0) {
                $code = Hint::AUTH_ERROR;
                $msg = Hint::getErrCodeMsg($code);
                break;
            }
            $ids = explode(',',$this->requestData['id']);
            $data = 0;
            foreach ($ids as $id){
                $res = $this->modelRoomImageCheck->CUDTable([] ,'delete' ,$id);
                if($res){
                    $data++;
                }
            }
        }while (false);

        return compact('code', 'msg', 'data');
    }

    public function sendImgMsg(){
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];
        do {
            $this->I('id', 'id', 'int', true);
            $this->I('room_id', '房间id', 'int', true);
            $variables = [];
            $checkParams = $this->check($this->requestData, $variables); //校验数据
            extract($checkParams);
            if ($code !== Hint::SUCCESS) {
                break;
            }
            $roomImgCheck = $this->modelRoomImageCheck->getInfo($this->requestData['id']);
            if(!empty($roomImgCheck['image'])){
                $user = $this->user->get_current_user();
                $data['user_id'] = $user['user_id'];
                $data['image'] = $roomImgCheck['image'];
            }
        } while(false);

        return compact('code', 'msg', 'data');
    }
}